package com.djf.djfcs.controller;

import com.djf.djfcs.canal.CanalClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Canal管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/canal")
@Api(tags = "Canal管理")
public class CanalController {
    
    @Autowired
    private CanalClientService canalClientService;
    
    /**
     * 获取Canal客户端状态
     */
    @GetMapping("/status")
    @ApiOperation("获取Canal客户端状态")
    public Map<String, Object> getStatus() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "Canal客户端正在运行中");
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
    
    /**
     * 重启Canal客户端
     */
    @PostMapping("/restart")
    @ApiOperation("重启Canal客户端")
    public Map<String, Object> restart() {
        Map<String, Object> result = new HashMap<>();
        try {
            log.info("手动重启Canal客户端...");
            
            // 停止当前客户端
            canalClientService.stop();
            
            // 等待一段时间
            Thread.sleep(2000);
            
            // 重新启动
            canalClientService.start();
            
            result.put("success", true);
            result.put("message", "Canal客户端重启成功");
            log.info("Canal客户端重启成功");
            
        } catch (Exception e) {
            log.error("重启Canal客户端失败", e);
            result.put("success", false);
            result.put("message", "重启失败: " + e.getMessage());
        }
        
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
    
    /**
     * 获取Canal配置信息
     */
    @GetMapping("/config")
    @ApiOperation("获取Canal配置信息")
    public Map<String, Object> getConfig() {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> config = new HashMap<>();
        
        // 这里可以返回当前的Canal配置信息
        config.put("server", "192.168.247.134:11111");
        config.put("destination", "example");
        config.put("batchSize", 1000);
        config.put("timeout", 1000);
        
        result.put("success", true);
        result.put("data", config);
        result.put("timestamp", System.currentTimeMillis());
        
        return result;
    }
}